import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/login',
      component: () => import('@/views/LoginHome')
    },
    {
      path: '/404',
      component: () => import('@/views/404Error')
    },
    {
      path: '/',
      component: () => import('@/views/layout/LayoutIndex'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/layout/home/HomeIndex')
        },
        {
          path: '/task/business',
          component: () => import('@/views/layout/task/TaskBusiness')
        },
        {
          path: '/task/operation',
          component: () => import('@/views/layout/task/TaskOperation')
        },
        {
          path: '/node/region',
          component: () => import('@/views/layout/node/NodeRegion')
        },
        {
          path: '/node/node',
          component: () => import('@/views/layout/node/NodeNode')
        },
        {
          path: '/node/partner',
          component: () => import('@/views/layout/node/NodePartner')
        },
        {
          path: '/vm/index',
          component: () => import('@/views/layout/vm/VmIndex')
        },
        {
          path: '/vm/status',
          component: () => import('@/views/layout/vm/VmStatus')
        },
        {
          path: '/vm/type',
          component: () => import('@/views/layout/vm/VmType')
        },
        {
          path: '/user/index',
          component: () => import('@/views/layout/user/UserIndex')
        },
        {
          path: '/user/user-task-stats',
          component: () => import('@/views/layout/user/UserStats')
        },
        {
          path: '/user/user-work',
          component: () => import('@/views/layout/user/UserWork')
        },
        {
          path: '/sku/sku-class',
          component: () => import('@/views/layout/sku/SkuClass')
        },
        {
          path: '/sku/sku',
          component: () => import('@/views/layout/sku/SkuSku')
        },
        {
          path: '/policy',
          component: () => import('@/views/layout/policy/PolicyIndex')
        },
        {
          path: '/order',
          component: () => import('@/views/layout/order/OrderIndex')
        },
        {
          path: '/report',
          component: () => import('@/views/layout/report/ReportIndex')
        }
      ]
    },
    { path: '*', redirect: '/404', hidden: true }
  ]
})
// 没有 token 或者不在白名单页面就跳到 login 页
const whiteList = ['/login', '/404']
router.beforeEach((to, from, next) => {
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
